package com.server.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.DStockTradeAvg;
import com.entity.DStockTradeDaily2025;
import com.entity.DStockTradeToday;
import com.mapper.db4.DStockTradeDaily2025Mapper;
import com.server.IDStockTradeAvgService;
import com.server.IDStockTradeDaily2025Service;
import com.server.IDStockTradeTodayService;
import com.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 历史日K数据 服务实现类
 * </p>
 *
 * @author chl
 * @since 2025-05-23
 */
@Service
public class DStockTradeDaily2025ServiceImpl extends ServiceImpl<DStockTradeDaily2025Mapper, DStockTradeDaily2025> implements IDStockTradeDaily2025Service {

    @Autowired
    private DStockTradeDaily2025Mapper dStockTradeDaily2025Mapper;
    @Autowired
    private IDStockTradeTodayService todayService;

    @Autowired
    private IDStockTradeAvgService idStockTradeAvgService;
    @Override
    public List<DStockTradeDaily2025> maxValue(String startDate, String endDate) {
        return dStockTradeDaily2025Mapper.maxValue(startDate, endDate);
    }

    @Override
    public List<DStockTradeDaily2025> maxValue2(String day) {
        return dStockTradeDaily2025Mapper.maxValue2(day);
    }

    @Override
    public List<DStockTradeDaily2025> minValue(String startDate, String endDate) {
        return dStockTradeDaily2025Mapper.minValue(startDate, endDate);
    }

    @Override
    public List<DStockTradeDaily2025> minValue2(String day) {
        return dStockTradeDaily2025Mapper.minValue2(day);
    }

    @Override
    public void saveAvgData() {
        Date date = new Date();
        Date datestart = DateUtils.addDay(date, -5);
        Date dateEnd = DateUtils.addDay(date, -1);
        List<DStockTradeDaily2025> avgData = dStockTradeDaily2025Mapper.selectByDay("6");
        Map<String, List<DStockTradeDaily2025>> collect = avgData.stream().collect(Collectors.groupingBy(DStockTradeDaily2025::getTsCode));

        // 今日数据
        List<DStockTradeToday> list = todayService.list();

        List<DStockTradeAvg> avgList = new ArrayList<>();
        for (DStockTradeToday dStockTradeToday : list) {
            String tsCode = dStockTradeToday.getTsCode();
            BigDecimal add = BigDecimal.valueOf(dStockTradeToday.getClosee());
            DStockTradeAvg avg = new DStockTradeAvg();
            avg.setTsCode(tsCode);
            avg.setName(dStockTradeToday.getName());
            avg.setTradeDate(DateUtils.format(date, "yyyyMMdd"));
            int count = 1;
            if (collect.containsKey(tsCode)) {
                for (DStockTradeDaily2025 d : collect.get(tsCode)) {
                    add = add.add(BigDecimal.valueOf(d.getClose()));
                    count++;
                }
            }

            BigDecimal avg5 = add.divide(BigDecimal.valueOf(count), 2, BigDecimal.ROUND_UP);
            avg.setAvg5(avg5.doubleValue());
            avgList.add(avg);

        }

        idStockTradeAvgService.saveBatch(avgList);

//        log.info("模型2分析完的股票数量为：" + todayList.size());
    }
}
